package com.changgou.pay.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.pay.service.WXPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/wxpay")
@RestController
public class WXPayController {

  @Autowired private WXPayService wxPayService;
  @Autowired private RabbitTemplate rabbitTemplate;
  // 下单

  /**
   * 普通用户 exchange:exchang.order routingKey: queue.order
   * 秒杀用户
   * exchange:exchang.seckillorder
   * routingKey: queue.seckillorder
   * @param parampMap
   * @return
   */
  @GetMapping("/nativePay")
  public Result nativePay(@RequestParam() Map<String, String> parampMap) {
    Map resultMap = wxPayService.nativePay(parampMap);
    return new Result(true, StatusCode.OK, "下单成功", resultMap);
  }
  /** 查询支付状态 */
  @GetMapping("/query/status")
  public Result queryState(@RequestParam String outtrandno) {
    Map resultMap = wxPayService.queryState(outtrandno);
    return new Result(true, StatusCode.OK, "查询支付状态成功", resultMap);
  }
  /** 支付结果的通知 */
  @GetMapping("/url")
  public Result notifUrl(HttpServletRequest request) throws Exception {
    // 因为这里无法申请微信支付，所以先注释掉
/*    // 获取网络的输入流
    ServletInputStream inputStream = request.getInputStream();
    // 创建一个output对象
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inputStream.read(buffer)) != -1) {
      baos.write(buffer, 0, len);
    }
    // 微信支付的字节数组
    byte[] bytes = baos.toByteArray();
    String xmlRequest = new String(bytes, "UTF-8");
    System.out.println(xmlRequest);
    Map<String, String> map = WXPayUtil.xmlToMap(xmlRequest);
    // 获得自定义数据
  String  attach = map.get("attach");
     Map<String,String> map =JSON.parseObject(attach, Map.class);
      rabbitTemplate.convertAndSend( map.get("exchange"), map.get("routingkey"),JSON.toJSONString(map));

    */

   Map<String,String> map = new HashMap<>();
    map.put("return_code", "SUCCESS");
    map.put("out_trade_no", "1236105074183241728");
    map.put("result_code", "SUCCESS");
    System.out.println(map);

    // 使用rbmq发送支付结果geimq(因为无法调用微信支付这里写假数据（秒杀用户     ）)
      /**
       * 普通用户
       */
    rabbitTemplate.convertAndSend("exchange.order", "queue.order",JSON.toJSONString(map));
      /**
       * 秒杀用户这里先写死
       */
      rabbitTemplate.convertAndSend("exchange.seckillorder", "queue.seckillorder",JSON.toJSONString(map));
   /* String result =
        "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    return result;*/
   return  new Result(true,StatusCode.OK,"支付成功",map);
  }
}
